package com.sky.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    /**
     *修改菜品
     * @param dishDTO
     * */
    @Override
    public void modifyDishes(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
        List<DishFlavor> flavors = dishDTO.getFlavors();
        DishFlavor dishFlavor = new DishFlavor();
        //先清空，再插入
        dishFlavorMapper.delete(dishDTO.getId());
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishDTO.getId());
            dishFlavor.setDishId(dishDTO.getId());
            dishFlavorMapper.addFlavors(flavor);
        }
    }

    /**
     * 添加菜品
     * */
    @Override
    public void addDishes(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.addDishes(dish);

        List<DishFlavor> flavors = dishDTO.getFlavors();
        DishFlavor dishFlavor = flavors.get(0);
        dishFlavor.setDishId(dish.getId());
        dishFlavorMapper.addFlavors(dishFlavor);
    }

    /**
     * @param dishPageQueryDTO
     * 菜品分页查询
     * @return
     * */
    @Override
    public PageResult dishPageQuery(DishPageQueryDTO dishPageQueryDTO) {
        //开启分页
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        log.info("dishPageQueryDTO.getStatus() {}",dishPageQueryDTO.getStatus());
        List<DishVO> dishVOS = dishMapper.queryDish(dishPageQueryDTO.getName(),dishPageQueryDTO.getCategoryId(),dishPageQueryDTO.getStatus());
        PageInfo<DishVO> dishPageInfo = new PageInfo<>(dishVOS);
        long total = dishPageInfo.getTotal();
        PageResult pageResult = new PageResult();
        pageResult.setTotal(total);
        pageResult.setRecords(dishVOS);
        return pageResult;
    }

    /**
     * @param ids
     * 批量删除菜品
     * @return
     * */
    @Override
    public void deleteByids(String ids) {
        String[] split = ids.split(",");
        Long id;
        for (int i = 0; i < split.length; i++) {
            id= Long.valueOf(split[i]);
            dishMapper.delete(id);
            dishFlavorMapper.delete(id);
        }

    }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     * */
    @Override
    public DishVO selectById(Long id) {
        DishVO dishVO = dishMapper.selectById(id);
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectById(id);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * */
    @Override
    public List<Dish> selectByCategoryId(Integer categoryId) {
        List<Dish> dishes = dishMapper.selectByCategoryId(categoryId);
        return dishes;
    }

    @Override
    public void changeStatus(Integer status,Long id) {
        dishMapper.changeStatus(status,id);
    }

    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
        List<DishVO> dishVOList = dishMapper.list(dish);
        for (int i = 0;i<dishVOList.size();i++){
            List<DishFlavor> dishFlavors = dishFlavorMapper.selectById(dishVOList.get(i).getId());
           dishVOList.get(0).setFlavors(dishFlavors);
        }
        return dishVOList;
    }
}





























